﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Samples.ServiceHosting.StorageClient;
using System.IO;

namespace AzureStorage.Manager
{
    public class FileBlobManager
    {
        private static BlobStorage _blobStorage;
        
        private const int NumRetries = 3;
       

        public FileBlobManager ()
	    {

	    }
        public static void Init()
        {
            StorageAccountInfo blobAccount = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration();
            try 
            {
                //Initialise le stockage pour les blobs
                _blobStorage = BlobStorage.Create(blobAccount);
                _blobStorage.RetryPolicy = RetryPolicies.RetryN(NumRetries, TimeSpan.FromMilliseconds(100));
                _blobStorage.Timeout = TimeSpan.FromMinutes(5);
	        }
	        catch (Exception)
	        {
        		
		        throw;
	        }
          
        }
        public static string DefaultContainerName;

        public static bool PutFileInBlob(string ContainerName,Byte[] FileContent,String FileName)
        {
            //Crée le container s'il n'existe pas 
            BlobContainer container = _blobStorage.GetBlobContainer(ContainerName);
            container.CreateContainer();
            

            BlobProperties BlobProp = new BlobProperties(FileName);
            BlobContents  BlobContent = new BlobContents(FileContent);

            //Renvoie true si le blob à été créé false si le fichier existe déja
            return container.CreateBlob(BlobProp, BlobContent, false);
        }

        public static bool PutFileInBlob(Byte[] FileContent, String FileName)
        {
            return PutFileInBlob(DefaultContainerName, FileContent, FileName);
        }

        public static IEnumerable<String> getListFiles(string ContainerName)
        {
            try
            {
                BlobContainer container = _blobStorage.GetBlobContainer(ContainerName);
                container.CreateContainer();
                return container.ListBlobs("", false).Select(MyBlobProp => ((BlobProperties)MyBlobProp).Name);
            }
            catch(Exception)
            {
                
                return null;
            }
        }

        public static IEnumerable<String> getListFiles()
        {
            return getListFiles(DefaultContainerName);
        }

        public static MemoryStream GetFileInBlob(String ContainerName,String FileName)
        {
            try
            {
                BlobContainer container = _blobStorage.GetBlobContainer(ContainerName);
                MemoryStream outputStream = new MemoryStream();

                container.GetBlob(FileName, new BlobContents(outputStream), false);

                outputStream.Seek(0, SeekOrigin.Begin);
                return outputStream;
            }
            catch (Exception)
            {

                return null;
            }
        }

        public static MemoryStream GetFileInBlob(String FileName)
        {
            return GetFileInBlob(DefaultContainerName,FileName);
        }

        public static bool DeleteFileInBlob(string ContainerName,string Filename)
        {
            BlobContainer container = _blobStorage.GetBlobContainer(ContainerName);
            return container.DeleteBlob(Filename);
        }
        public static bool DeleteFileInBlob(string Filename)
        {
            return DeleteFileInBlob(DefaultContainerName, Filename); 
        }

        public static IEnumerable<BlobProperties> GetBlobProps(string ContainerName)
        {
            BlobContainer container = _blobStorage.GetBlobContainer(ContainerName);
            return (IEnumerable<BlobProperties>)container.ListBlobs("", false).Select(MyBlobProp => ((BlobProperties)MyBlobProp));
        }
        public static IEnumerable<BlobProperties> GetBlobProps()
        {
            return GetBlobProps(DefaultContainerName);
        }
    }
}
